﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.ComponentModel;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using AMobiSDK;

namespace Sai_Se_Dung
{
    public partial class Confirm : PhoneApplicationPage
    {
        public static bool isBacked = false;

        public Confirm()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            AdManager.setWidgetCode("f2257ad5570a4a773a2edd33504ef3c2");//Bạn phải thay mã này bằng widgetCode của bạn
            AdManager advertisement = new AdManager();
            advertisement.OnNavigationTo(this, null, null, banner);
        }

        private void PlayButtonSound()
        {
            if (Option.isOn == true)
            {
                mp3_btn.Play();
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            PlayButtonSound();

            MessageBox.Show("Please press the BACK key on your phone one more time ^^", "How to exit the game =))", MessageBoxButton.OK);
            
            if (NavigationService.CanGoBack)
            {
                while (NavigationService.RemoveBackEntry() != null)
                {
                    NavigationService.RemoveBackEntry();
                }
            }

            btnAgree.IsEnabled = false;
            btnCancel.IsEnabled = false;
            
            isBacked = true;
        }

        private void btnAgree_Click(object sender, RoutedEventArgs e)
        {
            PlayButtonSound();

            MessageBox.Show("Don't forget that you are playing FALSE is TRUE game =))", "Hahahahh :-p", MessageBoxButton.OK);

            if (NavigationService.CanGoBack)
            {
                NavigationService.GoBack();
            }
            else
            {
                // Do nothing
            }

            //if (NavigationService.CanGoBack)
            //{
            //    while (NavigationService.RemoveBackEntry() != null)
            //    {
            //        NavigationService.RemoveBackEntry();
            //    }
            //}
            //NavigationService.Navigate(new Uri("/Menu.xaml", UriKind.Relative));
        }

        /// <summary>
        /// Ở đây override cũng được, không cũng được =))
        /// </summary>
        /// <param name="e"></param>
        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            if (Option.isOn == true)
            {
                mp3_btn.Play();
            }

            if (isBacked == false)
            {
                e.Cancel = true; // Vô hiệu hoá nút BACK khi chưa chọn nút "Không" trên Confirm =))
            }
            else
            {
                if (NavigationService.CanGoBack)
                {
                    while (NavigationService.RemoveBackEntry() != null)
                    {
                        NavigationService.RemoveBackEntry();
                    }
                }
            }
        }
    }
}